package com.ruoyi.project.tool.swagger.UserController;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.annotation.SignatureValidation;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.interceptor.annotation.RepeatSubmit;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.domain.SysAddress;
import com.ruoyi.project.system.domain.SysCity;
import com.ruoyi.project.system.service.ISysAddressService;
import com.ruoyi.project.system.service.ISysCityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 地址Controller
 *
 * @author jjp
 * @date 2020-04-22
 */
@RestController
@Api(description = "地址Controller")
@RequestMapping("/mobileUser/address")
public class UserAddressController extends BaseController {
    @Autowired
    private ISysAddressService sysAddressService;

    @Autowired
    private ISysCityService sysCityService;

    /**
     * 查询地址列表
     */
    @ApiOperation("查询用户已添加地址列表")
    @GetMapping("/list")
    public TableDataInfo list(SysAddress sysAddress) {
        sysAddress.setMobileUserId(SecurityUtils.getLoginUser().getSysMobileUser().getMobileUserId());
        List<SysAddress> list = new ArrayList<>();
        startPage();
        try {
            list = sysAddressService.selectSysAddressList(sysAddress);
            for (int i = 0; i < list.size(); i++) {
                Long area = Long.valueOf(list.get(i).getArea());
                SysCity areaCity = sysCityService.selectSysCityById(area);
                String areaName = areaCity.getCityName();
                SysCity cityCity = sysCityService.selectSysCityById(areaCity.getParentCityId());
                String cityName = cityCity.getCityName();
                SysCity sysCity = sysCityService.selectSysCityById(cityCity.getParentCityId());
                String provinceName = sysCity.getCityName();
                String allName = provinceName + cityName + areaName;
                list.get(i).setArea(allName);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return getDataTable(list);
    }

    /**
     * 获取地址详细信息
     */
    @ApiOperation("获取地址详细信息")
    @GetMapping(value = "/{addressId}")
    public AjaxResult getInfo(@PathVariable("addressId") Long addressId) {
        SysAddress sysAddress = sysAddressService.selectSysAddressById(addressId);
        Long area = Long.valueOf(sysAddress.getArea());
        SysCity areaCity = sysCityService.selectSysCityById(area);
        //区名称
        String cityName1 = areaCity.getCityName();
        Long cityId = areaCity.getParentCityId();
        SysCity province = sysCityService.selectSysCityById(cityId);
        //市名称
        String cityName2 = province.getCityName();

        SysCity sysCity2 = sysCityService.selectSysCityById(province.getParentCityId());
        //省名称
        String ProvinceName = sysCity2.getCityName();

        String cityName = ProvinceName + cityName2 + cityName1;

        sysAddress.setArea(cityName);

        Map map = new HashMap();
        map.put("sysAddress", sysAddress);
        map.put("areaId", area);

        return AjaxResult.success(map);
    }

    /**
     * 新增地址
     */
    @RepeatSubmit
    @SignatureValidation
    @ApiOperation("新增地址")
    @Log(title = "地址", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysAddress sysAddress) {
        sysAddress.setMobileUserId(SecurityUtils.getLoginUser().getSysMobileUser().getMobileUserId());
        if("1".equals(sysAddress.getDefalut())){
            sysAddressService.insertSysAddress(sysAddress);
            sysAddressService.updatesetNotdefault(sysAddress);
        }else{
            sysAddressService.insertSysAddress(sysAddress);
        }
        return AjaxResult.success("新增地址成功");
    }

    /**
     * 修改地址
     */
    @RepeatSubmit
    @SignatureValidation
    @ApiOperation("修改地址")
    @Log(title = "地址", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysAddress sysAddress) {
        sysAddress.setMobileUserId(SecurityUtils.getLoginUser().getSysMobileUser().getMobileUserId());
        sysAddressService.updatesetNotdefault(sysAddress);
        return toAjax(sysAddressService.updateSysAddress(sysAddress));
    }

    /**
     * 删除地址
     */
    @RepeatSubmit
    @SignatureValidation
    @ApiOperation("删除地址")
    @Log(title = "地址", businessType = BusinessType.DELETE)
    @DeleteMapping("/{addressId}")
    public AjaxResult remove(@PathVariable("addressId") Long addressId) {
        return toAjax(sysAddressService.UpateSysAddressById(addressId));
    }

    /**
     * 查询用户所有地址列表
     */
    @SignatureValidation
    @ApiOperation("查询地址列表")
//    @GetMapping("/allList/{mobileUserId}")
    public TableDataInfo allList(@PathVariable("mobileUserId") Long mobileUserId) {
        startPage();
        List<SysAddress> list = sysAddressService.selectSysAddressAllList(mobileUserId);
        for (int i = 0; i < list.size(); i++) {
            Long area = Long.valueOf(list.get(i).getArea());
            SysCity sysCity1 = sysCityService.selectSysCityById(area);
            String cityName1 = sysCity1.getCityName();
            String cityName2 = null;
            if (sysCity1.getParentCityId() != null) {
                SysCity sysCity2 = sysCityService.selectSysCityById(sysCity1.getParentCityId());
                cityName2 = sysCity2.getCityName();
            }
            String cityName = cityName2 + cityName1;
            list.get(i).setArea(cityName);
        }
        return getDataTable(list);
    }

    @ApiOperation("查询城市列表")
    @PostMapping("/getCity")
    public AjaxResult getCity(@RequestBody SysCity sysCity) {
        if (sysCity.getLevel() != null) {
            List<SysCity> sysCities = sysCityService.selectSysParentCityId(sysCity.getLevel());
            return AjaxResult.success(sysCities);
        }
        if (sysCity.getParentCityId() != null) {
            List<SysCity> sysCities = sysCityService.selectSysCityList(sysCity);
            return AjaxResult.success(sysCities);
        }
        List<SysCity> sysCities = sysCityService.selectSysCityList(sysCity);
        return AjaxResult.success(sysCities);
    }

    @ApiOperation("获取默认地址")
    @GetMapping("/getDefaultAddress")
    public AjaxResult getDefaultAddress() {
        SysAddress sysAddress = new SysAddress();
        sysAddress.setDefalut("1");
        sysAddress.setMobileUserId(SecurityUtils.getLoginUser().getSysMobileUser().getMobileUserId());
        List<SysAddress> addressList = sysAddressService.selectSysAddressList(sysAddress);
        return AjaxResult.success(addressList);
    }

}
